﻿using DbFactory;
using PlaneClean.CommonHelp;
using PlaneClean.Models;
using PlaneClean.ViewModels;
using PlaneCleanEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    /// <summary>
    /// 飞机管理
    /// </summary>
    public class PlaneController:ManagerController
    {
        /// <summary>
        /// 查询飞机列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<t_plane> List(PageListRequest<t_plane> request)
        {
            if (request.pageIndex <= 0) request.pageIndex = 1;
            if (request.pageSize <= 0) request.pageSize = 100;
            int total = 0;
            ListResponse<t_plane> response = new ListResponse<t_plane>();
            response.bodyList = new List<t_plane>();
            response.result = "NG";
            var sugar = DbFactory.dbFactory.Db.Queryable<t_plane>()
                .Where(a => a.p_id > 0);
            if (!string.IsNullOrEmpty(request.body.p_name))
            {
                sugar.Where(a => a.p_name.Contains(request.body.p_name));
            }
            //加上where 加快查询速度 (where 中使用索引条件)
            List<t_plane> planeList = sugar.ToPageList(request.pageIndex, request.pageSize, ref total);

            response.bodyList.AddRange(planeList);
            response.total = total;
            
            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 新增飞机
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Add(BaseRequest<t_plane> request)
        {
            BaseResponse response = new BaseResponse();
            if (request.body.p_id> 0)
            {
                response.reason = "有主键请调用更新接口";
                return response;
            }
          int pId=  DbFactory.dbFactory.Db.Insertable(request.body).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity();
            if (pId <= 0)
            {
                response.reason = "插入数据库异常,新增失败";
                return response;
            }
            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 修改飞机
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Update(BaseRequest<t_plane> request)
        {
            BaseResponse response = new BaseResponse();
            response.result = "NG";
            //if (request.body == null)
            //{
            //    response.reason = "请求body不能为空";
            //    return response;
            //}
            if (request.body.p_id <=0 )
            {
                response.reason = "没有主键请调用新增接口";
                return response;
            }
            t_plane plane = DbFactory.dbFactory.Db.Queryable<t_plane>().First(a => a.p_id == request.body.p_id);
            if (plane == null)
            {
                response.reason = "需要更新的飞机记录不存在";
                return response;
            }
            DbFactory.dbFactory.Db.Updateable(request.body).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();

            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Delete(BaseDeleteRequest request)
        {
            BaseResponse response = new BaseResponse();
            if (request.idList == null || request.idList.Count <= 0)
            {
                response.reason = "请传入需要删除飞机的主键";
                return response;
            }
            List<t_clean_plane> cpList = dbFactory.Db.Queryable<t_clean_plane>().Where(a => request.idList.Contains(a.p_id??0)).ToList();
            if (cpList != null && cpList.Count > 0)
            {
                response.reason = "飞机配置存在相关记录,删除失败";
                return response;
            }
            dbFactory.Db.Deleteable<t_plane>().Where(a =>  request.idList.Contains(a.p_id)).ExecuteCommand();
            response.result = "OK";
            return response;
        }
    }
}